package com.zixin.main;

import com.zixin.util.DruidUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DruidGenKeyWin {

    private JFrame frmSm;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DruidGenKeyWin window = new DruidGenKeyWin();
                    window.frmSm.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public DruidGenKeyWin() {
        initialize();
    }

    public boolean isEmpty(String text) {
        if (text == null || "".equals(text)) {
            return true;
        }
        return false;
    }

    public boolean isMoreThan53(String text) {
        return text.length() > 53 ? true : false;
    }


    public static boolean isContainChinese(String text) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(text);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        int width = 667;
        int height = 443;

        frmSm = new JFrame();
        frmSm.setResizable(false);
        frmSm.setIconImage(Toolkit.getDefaultToolkit().getImage("..\\image\\logo.png"));
        frmSm.setTitle("druid 加密");

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double widthFull = screenSize.getWidth();
        double heightFull = screenSize.getHeight();

        // 屏幕的物理大小还需要知道屏幕的dpi 意思是说一英寸多少个象素
        double dpi = Toolkit.getDefaultToolkit().getScreenResolution();
        double a = widthFull / dpi;
        double b = heightFull / dpi;
        System.out.println("屏幕尺寸是：" + Math.pow(a * a + b * b, 2));

        int x = (int) ((widthFull - width) / 2);
        int y = (int) ((heightFull - height) / 2);

        frmSm.setBounds(x, y, 667, 443);
        frmSm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmSm.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("\u52A0\u5BC6\u89E3\u5BC6\u5185\u5BB9\uFF1A");
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 23));
        lblNewLabel.setBounds(10, 10, 192, 36);
        frmSm.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\u89E3\u5BC6\u52A0\u5BC6\u7ED3\u679C\uFF1A");
        lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 23));
        lblNewLabel_1.setBounds(10, 145, 192, 43);
        frmSm.getContentPane().add(lblNewLabel_1);

        JTextArea material = new JTextArea();
        material.setBounds(10, 49, 630, 86);
        frmSm.getContentPane().add(material);

        JTextArea result = new JTextArea();
        result.setLineWrap(true);
        result.setBounds(10, 185, 630, 94);
        frmSm.getContentPane().add(result);

        JButton copyResult = new JButton("\u590D\u5236\u7ED3\u679C");
        copyResult.addActionListener(new ActionListener() {
            //复制结果
            public void actionPerformed(ActionEvent e) {
                String writeMe = result.getText();
                Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
                Transferable tText = new StringSelection(writeMe);
                clip.setContents(tText, null);
            }
        });
        copyResult.setFont(new Font("微软雅黑", Font.BOLD, 25));
        copyResult.setBounds(84, 342, 148, 43);
        frmSm.getContentPane().add(copyResult);

        JButton clearAll = new JButton("\u6E05\u7A7A\u5185\u5BB9");
        clearAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                material.setText("");
                result.setText("");
            }
        });
        clearAll.setFont(new Font("微软雅黑", Font.BOLD, 25));
        clearAll.setBounds(395, 342, 148, 43);
        frmSm.getContentPane().add(clearAll);

        JLabel lblNewLabel_2 = new JLabel("\u5982\u679C\u83B7\u53D6\u516C\u94A5\u6216\u79C1\u94A5\u4E0D\u9700\u8981\u586B\u5165\u4EFB\u4F55\u53C2\u6570");
        lblNewLabel_2.setForeground(Color.RED);
        lblNewLabel_2.setFont(new Font("宋体", Font.BOLD, 16));
        lblNewLabel_2.setBounds(168, 15, 332, 30);
        frmSm.getContentPane().add(lblNewLabel_2);

        JButton publicPsw = new JButton("\u660E\u6587\u52A0\u5BC6");
        publicPsw.addActionListener(new ActionListener() {
            /**
             * 获取加密密文
             */
            public void actionPerformed(ActionEvent e) {
                String text = material.getText();
                if (isEmpty(text)) {
                    result.setText("密码不能为空");
                    result.setForeground(new Color(255, 0, 0));
                    return;
                }
                if (isMoreThan53(text)) {
                    result.setText("密码长度不能超过53个字符");
                    result.setForeground(new Color(255, 0, 0));
                    return;
                }
                if (isContainChinese(text)) {
                    result.setText("密码不能包含中文字符");
                    result.setForeground(new Color(255, 0, 0));
                    return;
                }
                String encrypt = DruidUtil.encrypt(text);
                result.setText(encrypt);
                result.setForeground(new Color(0, 0, 0));
            }
        });
        publicPsw.setFont(new Font("微软雅黑", Font.BOLD, 25));
        publicPsw.setBounds(10, 289, 148, 43);
        frmSm.getContentPane().add(publicPsw);

        JButton privatePsw = new JButton("\u5BC6\u6587\u89E3\u5BC6");
        privatePsw.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                result.setText(DruidUtil.decrypt(material.getText()));
            }
        });
        privatePsw.setFont(new Font("微软雅黑", Font.BOLD, 25));
        privatePsw.setBounds(168, 289, 145, 43);
        frmSm.getContentPane().add(privatePsw);

        JButton privateKey = new JButton("\u83B7\u53D6\u79C1\u94A5");
        privateKey.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                result.setText(DruidUtil.getPrivateKey());
            }
        });
        privateKey.setFont(new Font("微软雅黑", Font.BOLD, 25));
        privateKey.setBounds(323, 289, 152, 43);
        frmSm.getContentPane().add(privateKey);

        JButton publicKey = new JButton("\u83B7\u53D6\u516C\u94A5");
        publicKey.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                result.setText(DruidUtil.getPublicKey());
            }
        });
        publicKey.setFont(new Font("微软雅黑", Font.BOLD, 25));
        publicKey.setBounds(485, 289, 145, 43);
        frmSm.getContentPane().add(publicKey);
    }
}
